Page({
    data: {
      popOpen: false,
      isbnCode: ''
    },
    // 打开弹框
    popOpen () {
      this.setData({
        popOpen: true
      })
    },
    // 关闭弹窗
    closePop () {
      this.setData({
        popOpen: false
      })
    },
    // 调起扫一扫

    getScancode () {
      var _this = this
      wx.scanCode({
        success: (res) => {
          console.log(res)
          if (res.errMsg == 'scanCode:ok') {
            wx.showToast({
              title: '扫码成功',
              icon: 'none'
            })
            var params = {
              isbnCode: res.result
            }
            _this.getBookuser(params)
          }else{
            wx.showToast({
              title: '扫码失败',
              icon: 'none'
            })
          }
        }
      })
    },


    // 手动输入bn吗
    bindCode (e) {
      this.setData({
        isbnCode: e.detail.value
      })
    },

    //

    sureAddbook () {
      var reg = /^[0-9]*$/
      if (this.data.isbnCode=='') {
        wx.showToast({
          title: 'ISBN码不能为空',
          icon: 'none'
        })
        return
      }

      if (!reg.test(this.data.isbnCode)) {
        wx.showToast({
          title: '请输入数字',
          icon: 'none'
        })
        return
      }

      var params = {
        isbnCode: this.data.isbnCode
      }
      this.getBookuser(params)
    },


    //接口

    // 手动输入
    getBookuser (params) {
      wx.request({
        url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/order/donate/getBookInfo/auth',
        data: params,
        method: 'POST',
        header: {
          "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token" : wx.getStorageSync('token'),
          "uid" : wx.getStorageSync('uuid')
        },
        success: (res) => {

          if (res.data.code == 200) {

            if (res.data.data != '' && res.data.data.price != 0 && res.data.data.price != '' && res.data.data.price != null && res.data.data.price != undefined) {

            wx.showToast({
              title: '添加成功',
              icon: 'none',
              duration: 1000
            })

        setTimeout(function () {
          var params = {
            id: res.data.data.id,
            name: res.data.data.name,
            serviceId: res.data.data.serviceId,
            author: res.data.data.author,
            price: res.data.data.price,
            coverPic: res.data.data.coverPic,
            maximumDiscount: res.data.data.expectedScore
          }
          var pages = getCurrentPages()
          var beforePage = pages[pages.length - 2]


          wx.navigateBack({
            success: function () {
              beforePage.addBook(params)
            }
          })
        },1000)
        }else{
          wx.navigateTo({
            url: '/package/juanshu/pages/shibieJieguo/shibieJieguo',
          })
        }
          }else{
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
          // console.log(res)
        }
      })
    }
})
